home *** CD-ROM | disk | FTP | other *** search
/ Enter 2007 April / ENTER_CD_04_07.iso / Internet / WinHTTrack 3.23 / httrack-3.23.exe / {app} / src / htsbase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-08  |  10.3 KB  |  391 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Basic definitions                                      */
  34. /*       Used in .c files for basic (malloc() ..) definitions   */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #ifndef HTS_BASICH
  39. #define HTS_BASICH
  40.  
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44.  
  45. #include "htsglobal.h"
  46.  
  47. // size_t et mode_t
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50.  
  51. #ifdef HAVE_UNISTD_H
  52. #include <unistd.h>
  53. #endif
  54. #ifdef HAVE_SYS_TYPES_H
  55. #include <sys/types.h>
  56. #endif
  57. #ifdef HAVE_SYS_STAT_H
  58. #include <sys/stat.h>
  59. #endif
  60. #ifdef HAVE_DLFCN_H
  61. #include <dlfcn.h>
  62. #endif
  63.  
  64. #if HTS_WIN
  65. #else
  66. #include <fcntl.h>
  67. #endif
  68. #include <assert.h>
  69.  
  70. #undef min
  71. #undef max
  72. #define min(a,b) ((a)>(b)?(b):(a))
  73. #define max(a,b) ((a)>(b)?(a):(b))
  74.  
  75. // teste ΘgalitΘ de 2 chars, case insensitive
  76. #define hichar(a) ((((a)>='a') && ((a)<='z')) ? ((a)-('a'-'A')) : (a))
  77. #define streql(a,b) (hichar(a)==hichar(b))
  78.  
  79. // is this MIME an hypertext MIME (text/html), html/js-style or other script/text type?
  80. #define HTS_HYPERTEXT_DEFAULT_MIME "text/html"
  81. #define is_hypertext_mime(a) \
  82.    ( (strfield2((a),"text/html")!=0)\
  83.   || (strfield2((a),"application/x-javascript")!=0) \
  84.   || (strfield2((a),"text/css")!=0) \
  85.   /*|| (strfield2((a),"text/vnd.wap.wml")!=0)*/ \
  86.   || (strfield2((a),"image/svg+xml")!=0) \
  87.   || (strfield2((a),"image/svg-xml")!=0) \
  88.   /*|| (strfield2((a),"audio/x-pn-realaudio")!=0) */\
  89.   )
  90.  
  91. #define may_be_hypertext_mime(a) \
  92.    (\
  93.      (strfield2((a),"audio/x-pn-realaudio")!=0) \
  94.   )
  95.  
  96.  
  97. // caractΦre maj
  98. #define isUpperLetter(a) ( ((a) >= 'A') && ((a) <= 'Z') )
  99.  
  100. // conversion Θventuelle / vers antislash
  101. #if HTS_WIN
  102. char* antislash(char* s);
  103. #else
  104. #define antislash(A) (A)
  105. #endif
  106.  
  107.  
  108. // functions
  109. #ifdef _WIN32
  110. #define DynamicGet(handle, sym) GetProcAddress(handle, sym)
  111. #else
  112. #define DynamicGet(handle, sym) dlsym(handle, sym)
  113. #endif
  114.  
  115. // emergency log
  116. typedef void (*t_abortLog)(char* msg, char* file, int line);
  117. extern HTSEXT_API t_abortLog abortLog__;
  118. #define abortLog(a) abortLog__(a, __FILE__, __LINE__)
  119. #define abortLogFmt(a) do { \
  120.   FILE* fp = fopen("CRASH.TXT", "wb"); \
  121.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb"); \
  122.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb"); \
  123.   if (fp) { \
  124.     fprintf(fp, "HTTrack closed at '" __FILE__ "', line %d\r\n", __LINE__); \
  125.     fprintf(fp, "Reason:\r\n"); \
  126.     fprintf(fp, a); \
  127.     fprintf(fp, "\r\n"); \
  128.     fflush(fp); \
  129.     fclose(fp); \
  130.   } \
  131. } while(0)
  132.  
  133.  
  134. #define _ ,
  135. #define abortLogFmt(a) do { \
  136.   FILE* fp = fopen("CRASH.TXT", "wb"); \
  137.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb"); \
  138.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb"); \
  139.   if (fp) { \
  140.     fprintf(fp, "HTTrack closed at '" __FILE__ "', line %d\r\n", __LINE__); \
  141.     fprintf(fp, "Reason:\r\n"); \
  142.     fprintf(fp, a); \
  143.     fprintf(fp, "\r\n"); \
  144.     fflush(fp); \
  145.     fclose(fp); \
  146.   } \
  147. } while(0)
  148. #define assertf(exp) do { \
  149.   if (! ( exp ) ) { \
  150.     abortLog("assert failed: " #exp); \
  151.     if (htsCallbackErr != NULL) { \
  152.       htsCallbackErr("assert failed: " #exp, __FILE__ , __LINE__ ); \
  153.     } \
  154.     assert(exp); \
  155.   } \
  156. } while(0)
  157. /* non-fatal assert */
  158. #define assertnf(exp) do { \
  159.   if (! ( exp ) ) { \
  160.     abortLog("assert failed: " #exp); \
  161.     if (htsCallbackErr != NULL) { \
  162.       htsCallbackErr("assert failed: " #exp, __FILE__ , __LINE__ ); \
  163.     } \
  164.   } \
  165. } while(0)
  166.  
  167.  
  168. /* regular malloc's() */
  169. #ifndef HTS_TRACE_MALLOC
  170. #define malloct(A)          malloc(A)
  171. #define calloct(A,B)        calloc(A,B)
  172. #define freet(A)            do { assertnf((A) != NULL); if ((A) != NULL) { free(A); (A) = NULL; } } while(0)
  173. #define realloct(A,B)       ( ((A) != NULL) ? realloc(A,B) : malloc(B) )
  174. #define memcpybuff(A, B, N) memcpy(A, B, N)
  175. #else
  176. /* debug version */
  177. #define malloct(A)    hts_malloc(A)
  178. #define calloct(A,B)  hts_calloc(A,B)
  179. #define freet(A)      do { hts_free(A); (A) = NULL; } while(0)
  180. #define realloct(A,B) hts_realloc(A,B)
  181. void  hts_freeall();
  182. void* hts_malloc    (size_t);
  183. void* hts_calloc(size_t,size_t);
  184. void* hts_xmalloc(size_t,size_t);
  185. void  hts_free      (void*);
  186. void* hts_realloc   (void*,size_t);
  187. mlink* hts_find(char* adr);
  188. /* protected memcpy */
  189. #define memcpybuff(A, B, N) do { \
  190.   mlink* lnk = hts_find((void*)(A)); \
  191.   if (lnk != NULL) { \
  192.     assertf(lnk != NULL); \
  193.     assertf( * ( (t_htsboundary*) ( ((char*) lnk->adr) - sizeof(htsboundary) ) ) == htsboundary ); \
  194.     assertf( * ( (t_htsboundary*) ( ((char*) lnk->adr) + lnk->len ) ) == htsboundary ); \
  195.     assertf( ( ((char*)(A)) + (N)) < (char*) (lnk->adr + lnk->len) ); \
  196.   } \
  197.   memcpy(A, B, N); \
  198. } while(0)
  199.  
  200. #endif
  201.  
  202. typedef void (* htsErrorCallback)(char* msg, char* file, int line);
  203. extern HTSEXT_API htsErrorCallback htsCallbackErr;
  204. extern HTSEXT_API int htsMemoryFastXfr;
  205.  
  206. /*
  207. */
  208.  
  209.  
  210. #ifdef STRDEBUG
  211.  
  212. /* protected strcat, strncat and strcpy - definitely useful */
  213. #define strcatbuff(A, B) do { \
  214.   assertf( (A) != NULL ); \
  215.   if ( ! (B) ) { assertf( 0 ); } \
  216.   if (htsMemoryFastXfr) { \
  217.     if (sizeof(A) != sizeof(char*)) { \
  218.       (A)[sizeof(A) - 1] = '\0'; \
  219.     } \
  220.     strcat(A, B); \
  221.     if (sizeof(A) != sizeof(char*)) { \
  222.       assertf((A)[sizeof(A) - 1] == '\0'); \
  223.     } \
  224.   } else { \
  225.     unsigned int sz = (unsigned int) strlen(A); \
  226.     unsigned int szf = (unsigned int) strlen(B); \
  227.     if (sizeof(A) != sizeof(char*)) { \
  228.       assertf(sz + szf + 1 < sizeof(A)); \
  229.       if (szf > 0) { \
  230.         if (sz + szf + 1 < sizeof(A)) { \
  231.           memcpy((A) + sz, (B), szf + 1); \
  232.         } \
  233.       } \
  234.     } else if (szf > 0) { \
  235.       memcpybuff((A) + sz, (B), szf + 1); \
  236.     } \
  237.   } \
  238. } while(0)
  239. #define strncatbuff(A, B, N) do { \
  240.   assertf( (A) != NULL ); \
  241.   if ( ! (B) ) { assertf( 0 ); } \
  242.   if (htsMemoryFastXfr) { \
  243.     if (sizeof(A) != sizeof(char*)) { \
  244.       (A)[sizeof(A) - 1] = '\0'; \
  245.     } \
  246.     strncat(A, B, N); \
  247.     if (sizeof(A) != sizeof(char*)) { \
  248.       assertf((A)[sizeof(A) - 1] == '\0'); \
  249.     } \
  250.   } else { \
  251.     unsigned int sz = (unsigned int) strlen(A); \
  252.     unsigned int szf = (unsigned int) strlen(B); \
  253.     if (szf > (unsigned int) (N)) szf = (unsigned int) (N); \
  254.     if (sizeof(A) != sizeof(char*)) { \
  255.       assertf(sz + szf + 1 < sizeof(A)); \
  256.       if (szf > 0) { \
  257.         if (sz + szf + 1 < sizeof(A)) { \
  258.           memcpy((A) + sz, (B), szf); \
  259.           * ( (A) + sz + szf) = '\0'; \
  260.         } \
  261.       } \
  262.     } else if (szf > 0) { \
  263.       memcpybuff((A) + sz, (B), szf); \
  264.       * ( (A) + sz + szf) = '\0'; \
  265.     } \
  266.   } \
  267. } while(0)
  268. #define strcpybuff(A, B) do { \
  269.   assertf( (A) != NULL ); \
  270.   if ( ! (B) ) { assertf( 0 ); } \
  271.   if (htsMemoryFastXfr) { \
  272.     if (sizeof(A) != sizeof(char*)) { \
  273.       (A)[sizeof(A) - 1] = '\0'; \
  274.     } \
  275.     strcpy(A, B); \
  276.     if (sizeof(A) != sizeof(char*)) { \
  277.       assertf((A)[sizeof(A) - 1] == '\0'); \
  278.     } \
  279.   } else { \
  280.     unsigned int szf = (unsigned int) strlen(B); \
  281.     if (sizeof(A) != sizeof(char*)) { \
  282.       assertf(szf + 1 < sizeof(A)); \
  283.       if (szf > 0) { \
  284.         if (szf + 1 < sizeof(A)) { \
  285.           memcpy((A), (B), szf + 1); \
  286.         } else { \
  287.           * (A) = '\0'; \
  288.         } \
  289.       } else { \
  290.         * (A) = '\0'; \
  291.       } \
  292.     } else { \
  293.       memcpybuff((A), (B), szf + 1); \
  294.     } \
  295.   } \
  296. } while(0)
  297. #define strncpybuff(A, B, N) do { \
  298.   assertf( (A) != NULL ); \
  299.   if ( ! (B) ) { assertf( 0 ); } \
  300.   if (htsMemoryFastXfr) { \
  301.     if (sizeof(A) != sizeof(char*)) { \
  302.       (A)[sizeof(A) - 1] = '\0'; \
  303.     } \
  304.     strncpy(A, B, N); \
  305.     if (sizeof(A) != sizeof(char*)) { \
  306.       assertf((A)[sizeof(A) - 1] == '\0'); \
  307.     } \
  308.   } else { \
  309.     unsigned int szf = (unsigned int) strlen(B); \
  310.     if (szf > (unsigned int) (N)) szf = (unsigned int) (N); \
  311.     if (sizeof(A) != sizeof(char*)) { \
  312.       assertf(szf + 1 < sizeof(A)); \
  313.       if (szf > 0) { \
  314.         if (szf + 1 < sizeof(A)) { \
  315.           memcpy((A), (B), szf); \
  316.         } \
  317.       } \
  318.     } else { \
  319.       memcpybuff((A), (B), szf); \
  320.     } \
  321.   } \
  322. } while(0)
  323.  
  324. #else
  325.  
  326. #ifdef STRDEBUGFAST
  327.  
  328. /* protected strcat, strncat and strcpy - definitely useful */
  329. #define strcatbuff(A, B) do { \
  330.   assertf( (A) != NULL ); \
  331.   if ( ! (B) ) { assertf( 0 ); } \
  332.   if (sizeof(A) != sizeof(char*)) { \
  333.     (A)[sizeof(A) - 1] = '\0'; \
  334.   } \
  335.   strcat(A, B); \
  336.   if (sizeof(A) != sizeof(char*)) { \
  337.     assertf((A)[sizeof(A) - 1] == '\0'); \
  338.   } \
  339. } while(0)
  340. #define strncatbuff(A, B, N) do { \
  341.   assertf( (A) != NULL ); \
  342.   if ( ! (B) ) { assertf( 0 ); } \
  343.   if (sizeof(A) != sizeof(char*)) { \
  344.     (A)[sizeof(A) - 1] = '\0'; \
  345.   } \
  346.   strncat(A, B, N); \
  347.   if (sizeof(A) != sizeof(char*)) { \
  348.     assertf((A)[sizeof(A) - 1] == '\0'); \
  349.   } \
  350. } while(0)
  351. #define strcpybuff(A, B) do { \
  352.   assertf( (A) != NULL ); \
  353.   if ( ! (B) ) { assertf( 0 ); } \
  354.   if (sizeof(A) != sizeof(char*)) { \
  355.     (A)[sizeof(A) - 1] = '\0'; \
  356.   } \
  357.   strcpy(A, B); \
  358.   if (sizeof(A) != sizeof(char*)) { \
  359.     assertf((A)[sizeof(A) - 1] == '\0'); \
  360.   } \
  361. } while(0)
  362. #define strncpybuff(A, B, N) do { \
  363.   assertf( (A) != NULL ); \
  364.   if ( ! (B) ) { assertf( 0 ); } \
  365.   if (sizeof(A) != sizeof(char*)) { \
  366.     (A)[sizeof(A) - 1] = '\0'; \
  367.   } \
  368.   strncpy(A, B, N); \
  369.   if (sizeof(A) != sizeof(char*)) { \
  370.     assertf((A)[sizeof(A) - 1] == '\0'); \
  371.   } \
  372. } while(0)
  373.  
  374. #else
  375.  
  376. #define strcatbuff strcat
  377. #define strncatbuff strncat
  378. #define strcpybuff strcpy
  379. #define strncpybuff strncpy
  380.  
  381. #endif
  382.  
  383. #endif
  384.  
  385.  
  386. #ifdef __cplusplus
  387.  };
  388. #endif
  389.  
  390. #endif
  391.